package com.zoke.dappstore.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.zoke.dappstore.R;


/**
 * 更新弹框
 *
 * @author
 */
public class UpdateDialog {
    private Activity activity;
    private AlertDialog dialog;
    private ButtonClickListener clickListener;

    TextView tv_confirm, tv_cancel, tv_desc, tv_version;

    public UpdateDialog(Activity activity) {
        this.activity = activity;
    }

    public void showDialog() {
        dialog = new AlertDialog.Builder(activity, R.style.update_dialog).create();
        //点击外部区域不能取消dialog
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(keyListener);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.layout_update_dialog);
        tv_desc = (TextView) window.findViewById(R.id.tv_desc);
        tv_confirm = (TextView) window.findViewById(R.id.tv_confirm);
        tv_cancel = (TextView) window.findViewById(R.id.tv_cancel);
        tv_version = (TextView) window.findViewById(R.id.tv_version);
        tv_confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (clickListener != null)
                    clickListener.onSureClick();
                dialog.dismiss();
            }
        });

        tv_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (clickListener != null)
                    clickListener.onCancelClick();
                dialog.dismiss();
            }
        });


        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);
    }

    public void disDialog() {
        if (dialog != null)
            dialog.dismiss();
    }

    public void showDesc(String desc, String version, String cancel) {
        tv_desc.setText(desc);
        tv_version.setText(version);
        tv_cancel.setText(cancel);
    }

    public interface ButtonClickListener {
        void onSureClick();

        void onCancelClick();
    }

    public void setClickListener(ButtonClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public static DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return true;
            } else {
                return false;
            }
        }
    };
}
